<?php

/**
search form for contact
class: myContactSearchForm
author: milwell
date: 2008-10-28 20:39
*/

  class myContactSearchForm extends sfForm{
    
    public function configure(){
    
      $this->setWidgets(
        array(
          'contact_category' => 
            new sfWidgetFormSelect(
              array(
                'choices'=>array_combine(
                  sfConfig::get('app_selection_contact_category'),
                  sfConfig::get('app_selection_contact_category')
                )
              )
            ),
          'company_name' => new sfWidgetFormInput(),
          'last_name' => new sfWidgetFormInput(),
          'first_name' => new sfWidgetFormInput(),
          'status' => 
            new sfWidgetFormSelect(
              array(
                'choices'=>array_combine(
                  array('ACTIVE','INACTIVE'),
                  array('ACTIVE','INACTIVE')
                )
              )
            ),
          'checkbox_customer' => new sfWidgetFormInputCheckbox(),
          'checkbox_supplier' => new sfWidgetFormInputCheckbox(),
          'checkbox_owner' => new sfWidgetFormInputCheckbox(),
        )
      );
      
      $this->setValidators(array(
        'contact_category' => new sfValidatorString(array('max_length' => 30)),
        'company_name'     => new sfValidatorString(array('max_length' => 100)),
        'first_name'       => new sfValidatorString(array('max_length' => 50)),
        'last_name'        => new sfValidatorString(array('max_length' => 50)),
        'status'        => new sfValidatorString(),
        'checkbox_customer'          => new sfValidatorString(),
        'checkbox_supplier'          => new sfValidatorString(),
        'checkbox_owner'          => new sfValidatorString(),
    ));
      
      $this->widgetSchema->setLabels(
        array(
          'contact_category' => 'Type',
          'company_name' => 'Company',
          'last_name' => 'Last Name',
          'first_name' => 'First Name',
          'checkbox_customer' => 'Customer',
          'checkbox_supplier' => 'Supplier',
          'checkbox_owner' => 'Owner',
        )
      );
      
      $this->widgetSchema['checkbox_customer']->setOption('value_attribute_value','Y');
      $this->widgetSchema['checkbox_supplier']->setOption('value_attribute_value','Y');
      $this->widgetSchema['checkbox_owner']->setOption('value_attribute_value','Y');
            
      $this->widgetSchema->setNameFormat('skey_contact[%s]');
      
    }
    
    public function search(){
      $c = new Criteria();
      
      if($this['contact_category']->getValue())
        $c->add(ContactPeer::CONTACT_CATEGORY,$this['contact_category']->getValue());

      if($this['company_name']->getValue())
        $c->add(ContactPeer::COMPANY_NAME,'%'.$this['company_name']->getValue().'%',Criteria::LIKE);
              
      if($this['last_name']->getValue())
        $c->add(ContactPeer::LAST_NAME,'%'.$this['last_name']->getValue().'%',Criteria::LIKE);
              
      if($this['first_name']->getValue())
        $c->add(ContactPeer::FIRST_NAME,'%'.$this['first_name']->getValue().'%',Criteria::LIKE);
              
      $join_count = 0;
      if(($this['checkbox_customer']->getValue() == 'Y') ||
         ($this['checkbox_supplier']->getValue() == 'Y') ||
         ($this['checkbox_owner']->getValue() == 'Y'))
      { 
        if($this['checkbox_customer']->getValue() == 'Y'){
          $c->addJoin(ContactPeer::IDEN,AccountCustomerPeer::CONTACT_IDEN);
          $join_count = $join_count + 1;
        }
        if($this['checkbox_supplier']->getValue() == 'Y'){
          $c->addJoin(ContactPeer::IDEN,AccountSupplierPeer::CONTACT_IDEN);
          $join_count = $join_count + 1;
        }
        if($this['checkbox_owner']->getValue() == 'Y'){
          $c->addJoin(ContactPeer::IDEN,AccountOwnerPeer::CONTACT_IDEN);
          $join_count = $join_count + 1;
        }
        if($join_count>1)
          $c->addGroupByColumn(ContactPeer::IDEN);
      }
      $c->add(ContactPeer::STATUS,$this['status']->getValue());
      $c->addAscendingOrderByColumn(ContactPeer::LIST_NAME);
      return ContactPeer::doSelect($c);
      
      
    }
    
    public function setCustomerMode(){
      $this->widgetSchema['checkbox_customer'] = new sfWidgetFormInputHidden();
      $this->setDefault('checkbox_customer','Y');
      $this->widgetSchema['checkbox_supplier'] = new sfWidgetFormInputHidden();
      $this->setDefault('checkbox_supplier','N');
      $this->widgetSchema['checkbox_owner'] = new sfWidgetFormInputHidden();
      $this->setDefault('checkbox_owner','N');
    }
    
    public function setSupplierMode(){
      $this->widgetSchema['checkbox_customer'] = new sfWidgetFormInputHidden();
      $this->setDefault('checkbox_customer','N');
      $this->widgetSchema['checkbox_supplier'] = new sfWidgetFormInputHidden();
      $this->setDefault('checkbox_supplier','Y');
      $this->widgetSchema['checkbox_owner'] = new sfWidgetFormInputHidden();
      $this->setDefault('checkbox_owner','N');
    }
    
    public static function getUrl(){
      return 'contact/doSearch';
    }
    
    public function renderFields(){?>
        <tr>
          <th class='field'>
            <?php echo $this['contact_category']->renderLabel()?>
          </th>
          <td class="field">
            <?php echo $this['contact_category']?>
          </td>
          <th class="field">
            <?php echo $this['company_name']->renderLabel()?>
          </th>
          <td class="field">
            <?php echo $this['company_name']?>
          </td>
          <th class="field">
            <?php echo $this['last_name']->renderLabel()?>
          </th>
          <td class="field">
            <?php echo $this['last_name']?>
          </td>
          <th class="field">
            <?php echo $this['first_name']->renderLabel()?>
          </th>
          <td class="field">
            <?php echo $this['first_name']?>
          </td>
          <th class="field">
            <?php echo $this['status']->renderLabel()?>
          </th>
          <td class="field">
            <?php echo $this['status']?>
          </td>
        </tr>
        <?php if(
                  $this['checkbox_customer']->isHidden() &&
                  $this['checkbox_supplier']->isHidden() &&
                  $this['checkbox_owner']->isHidden()
                ):
        ?>
          <?php echo $this['checkbox_customer']?>
          <?php echo $this['checkbox_supplier']?>
          <?php echo $this['checkbox_owner']?>
        <?php else:?>
        <tr>
          <th class="field">
            <?php echo $this['checkbox_customer']->renderLabel()?>
          </th>
          <td class="field">
            <?php echo $this['checkbox_customer']?>
          </td>
          <th class="field">
            <?php echo $this['checkbox_supplier']->renderLabel()?>
          </th>
          <td class="field">
            <?php echo $this['checkbox_supplier']?>
          </td>
          <th class="field">
            <?php echo $this['checkbox_owner']->renderLabel()?>
          </th>
          <td class="field">
            <?php echo $this['checkbox_owner']?>
          </td>
        </tr>
        <?php endif;?>
<?php }
    
  }


?>